package cn.ffcs.bpm.dao;


import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.bpm.model.NextTep;
import cn.ffcs.bpm.model.ProcessConfig;

/**
 * 
 * 流程DAO实现<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class NextTepDAO<D extends NextTep> extends BaseDAO<D> implements INextTepDAO<D>
{

    /**
     * 获取共享资料实体类
     * @param b
     * @return
     */
    public String getEntityName()
    {
        return new NextTep().getClass().getName();
    }
    
    
    public void remove(Object entity, String fParam,String fParamValue)
    {
        String hql = "delete from " + getEntityName()+" b where b."+ fParam
        + " = '" + fParamValue + "'";
        getSession().createQuery(hql).executeUpdate();
    }
    
    /**
     * 
     * {@inheritDoc}
     */
    public List<D> find(Object entity)
    {
        String hql = "select b.* from t_bpm_nexttep b start with b.process_detail='"+entity+"' connect by b.process_detail=prior b.next_config";
        return getSession().createSQLQuery(hql).addEntity("b",NextTep.class).list();
    }
//    String treeSql = "" + 
// 　　 "select , level from tree t " + 
// 　　 " start with t.parent_id = 0 " + 
// 　　 " connect by prior t.id = t.parent_id"; 
// 　　List result = session.createSQLQuery(treeSql) 
// 　　 .addEntity("t", Tree.class) 
// 　　 .addScalar("level", Hibernate.INTEGER) 
// 　　 .list(); 

    /**
     * 按查询条件获取总数
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<D> findBy(Criterion cond)
    {
        final Criterion conds=cond;
        return getHibernateTemplate().executeFind(
                new HibernateCallback()
                {
                    public List doInHibernate(Session session)
                            throws HibernateException
                    {
                        Criteria criteria = session.createCriteria(getEntityName());
                        criteria.add(conds);
                        return criteria.list();
                    }
                });
    }

}
